package org.redmagic.service.controller;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.redmagic.service.dao.PlaceholderDao;
import org.redmagic.service.domain.Property;
import org.redmagic.service.utils.PropertyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class GetPropertyController {

	private static final Log LOG  = LogFactory.getLog(GetPropertyController.class);
	
	@Autowired
	private PlaceholderDao dao;
	
	@RequestMapping(value="/property/**", method=RequestMethod.GET, produces="text/plain")
	public ResponseEntity<String> getProperty(HttpServletRequest request) {
		Property property = PropertyUtils.create(request, "/property/");
		logPropertyRequest(property);
		String value = dao.get(property);
		if (value != null) {
		    return new ResponseEntity<String>(value, HttpStatus.OK);		    
		} else {
		    return new ResponseEntity<String>(HttpStatus.NOT_FOUND);
		}
	}

    private void logPropertyRequest(Property property) {
        if (LOG.isInfoEnabled()) {
            LOG.info("[GET] Controller requesting value for..." + property);
            System.out.println("[GET] Controller requesting value for..." + property);
        }
    }
}
